package com.studytest.yiqilaidati.service.impl;

import com.studytest.yiqilaidati.dao.UserDao;
import com.studytest.yiqilaidati.modle.Respbody;
import com.studytest.yiqilaidati.modle.User;
import com.studytest.yiqilaidati.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {


    @Autowired
    private UserDao userDao;


    @Override
    public Respbody userLogin(String name, String password) {
        Respbody respbody = new Respbody();
        String number=null;
        try{
             number=userDao.findByName(name);
            if(number!=null&&number.equals(password)){
                respbody.setStatus(true);
                respbody.setCode("200");
                respbody.setMsg("执行成功");
            }else{
                respbody.setStatus(false);
                respbody.setCode("300");
            }
        }catch (Exception e){

                respbody.setStatus(false);
                respbody.setCode("301");
                respbody.setMsg("执行失败");

        }

        return respbody;
    }

    @Override
    public List<User> findAllUsers() {
        List<User> all = userDao.findAll();
        return all;
    }

    @Override
    public String saveUsers(User user) {
        HashMap map=new HashMap<String,Object>();
        int i = userDao.saveUser(user);
        if(i>0){
            map.put("result","success");
            return  map.toString();
        }else{
            return map.put("result","false").toString();
        }
    }

    @Override
    public String updateUsers(User user) {
        HashMap map=new HashMap<String,Object>();
        int i= userDao.updateUser(user);
        if(i>0){
            map.put("result","success");
            return  map.toString();
        }else{
            return map.put("result","false").toString();
        }

    }


}
